package com.base.sms.handler.factory;

import com.base.sms.handler.SendHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SendHandlerFactory implements HandlerFactory {

    private final Logger log = LoggerFactory.getLogger(SendHandlerFactory.class);
    private static final Map<String, SendHandler> handlerMap = new HashMap<>();
    // 构造函数，如果是集合接口对象，那么会把spring容器中所有关于该接口的子类，全部抓出来放入到集合中
    public SendHandlerFactory(List<SendHandler> strategyList){
        for (SendHandler strategy: strategyList) {
            handlerMap.put(strategy.support().toLowerCase(),strategy);
        }
    }

    @Override
    public SendHandler newInstance(String handler) {
        log.info("【短信实例】:"+ handlerMap.toString());
        SendHandler sendHandler = handlerMap.get(handler.toLowerCase());
        return sendHandler;
    }
}
